/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_view.js                                 
*  Description  : 개별클럽 일반게시판 상세보기
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-23  이윤노    Created.  
*  2012-05-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	// 상단 타이틀
	function clsClubTitle()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsClubTitle.prototype = {
		_init: function(){
			var $layer = $("div#container>section#main_content>div.golf_board article.depth2_tab");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$tabs": $layer.find("li")
			}
			
			$layer.data("idx", null); // 인덱스로 관리
			
			this.display();
		}
		, setIdx: function(val){
			
			var $layer = this._HTMLDom["$layer"];
			
			if(val == $layer.data("idx")) return false;
			
			$layer.data("idx", val);
			
			this.display();
			return true;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			var idx = $layer.data("idx");
			
			// 현재 인덱싱된 타이틀만 on설정
			$tabs.find("img").each(function(n){
				var imgSrc = $(this).attr("src");
				var szImgName = imgSrc.replace(/(.*)\/(.*).gif/g, function(){
					return arguments[1] + "/" + arguments[2].replace("_on", "");
				});
				
				$(this).attr("src", szImgName + ((idx == n) ? "_on":"") + ".gif");
			});
		}
		// 탭 버튼
		, $getsBtnTab: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			
			return $tabs.find("a");
		}
	}
	
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	function clsClubMember(){
		this._HTMLDom = null;
		this.memberData = null;
		
		this._init();
	}
	
	clsClubMember.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_list table.clublist_w  tbody");
			this._HTMLDom = {
				"$layer": $layer	
				,"$element": $layer.find("tr:first")
			}
			
			$layer.empty();
		}		
		, setData: function(listData){
			this.memberData = listData;
			this.display();	
		}
		, display: function(){
			var thisClass = this;
			/*
							<td><input type="checkbox" name="check" id="check"></td>
							<td>10</td>
							<td class="thumb">
								<span><img  src="../images/board/default_50_50.gif" alt=""  width="50" height="50" /></span>
								<span class="nickname">오스케 짱말짱</span>
							</td>
							<td>일반회원</td>
							<td>100</td>
							<td>50</td>
							<td>10</td>
							<td>2012-03-03</td>
							<td>2012-05-01</td>
							*/
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"]
			
			$layer.empty();
			
			for(var i=0;i<this.memberData.length;++i){
				$layer.append($element.clone(true));
			}
			
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0) input").data("회원키",thisClass.memberData[n]["회원키"]);
				$(this).find("td:eq(1)").html(thisClass.memberData[n]["회원키"]);
				$(this).find("td.thumb img").attr("src",thisClass.memberData[n]["회원정보"]["이미지"]);
				$(this).find("td.thumb span.nickname").html(thisClass.memberData[n]["회원정보"]["닉네임"]);
				$(this).find("td:eq(3)").html(thisClass.memberData[n]["회원구분"]);
				$(this).find("td:eq(4)").html(thisClass.memberData[n]["게시글수"]);
				$(this).find("td:eq(5)").html(thisClass.memberData[n]["댓글수"]);
				$(this).find("td:eq(6)").html(thisClass.memberData[n]["방문수"]);
				$(this).find("td:eq(7)").html(thisClass.memberData[n]["가입일"]);
				$(this).find("td:eq(8)").html(thisClass.memberData[n]["최종방문일"]);				
			});
		}	
		, spinStart: function(){
			
		}
		, spinEnd: function(){
			
		}
		, getCheckBoxKey: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			var checkKey = "";
			var nickname = "";
			
			$layer.find("input").each(function(n){				
					if($(this).attr("checked")) {
						if(checkKey!="") {
							checkKey+=",";
							nickname+=",";
						}
						checkKey+=thisClass.memberData[n]["회원키"];
						nickname+=thisClass.memberData[n]["회원정보"]["닉네임"];
					}
			});
			
			var result = {
				"회원키" : checkKey
				, "닉네임" : nickname
			};
			
			return result;
		}		
		, setChecked: function(key){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			var checkKey = "";
			var nickname = "";
			
			$layer.find("input").each(function(n){				
					$(this).attr("checked", key)
			});
		}
	}
	
	
	// 검색타입
	function clsSearchType()
	{
		this._HTMLDom = null;
		this.idx = null;
		
		this._init();
	}
	
	clsSearchType.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search div.selectlayer");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			this.idx = 0;
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0) a").click(function(event){
				event.preventDefault();
				
				$layer.find("ul").show(300);
			});
			
			$layer.find("li a").click(function(event){
				event.preventDefault();
				
				thisClass.setIdx($layer.find("li a").index($(this)));
			});
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0) a").html($layer.find("li a").eq(this.idx).html());
		}
		, setIdx: function(idx){
			this.idx = idx;
			this.display();
			this._HTMLDom["$layer"].find("ul").hide(300);
		}
		, $getSelect: function(){
			return this._HTMLDom["$layer"].find("ul");
		}
		, getValue: function(){
			return this.idx;
		}
	}
	
	// 검색어
	function clsSearchText()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "검색어를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsSearchText.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search :text");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, isValid: function(){
			this.errMsg = null;
			
			if(this.getValue() == ""){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	function clsComm()
	{
		this.clubData = null;
		this.clubRequest = null;
		
		this.clubMemberList = null;
		this.clubMemberRequest = null;
		this.clubMemberPageData = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubRequest = {
				"클럽키":null		
			}
		
			this.clubData = {
				"제목": null
				, "댓글수": null
				, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
				, "작성일": null
				, "작성시": null
				, "조회수": null
				, "내용": null
			}
			
			this.clubMemberList = {
				"회원키": null
				,"회원정보" : {
					"이미지" : null
					,"닉네임": null
				}
				,"회원구분":null
				,"게시글수" : null
				,"댓글수" : null
				,"방문수" : null
				,"가입일" : null
				,"최종방문일" : null	
			}
			
			this.clubMemberRequest = {
				"검색종류": null
				,"검색어": null
				,"페이지": null
			}
			
			this.clubMemberPageData = {
				"페이지": null	
			}
			
		}
		, getClubInfo: function(){
			var thisClass = this;
			var board_seq = this.getClubRequest("클럽키");
			
			//ajax
			setTimeout(function(){				
				thisClass.clubData = {
					"제목": "제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다~람쥐~."
					, "댓글수": 2
					, "작성자": {
						"아이디": "sky72"
						, "이미지": "../images/board/default_50_50.gif"
						, "닉네임": "오스케 짱말짱짱말짱"
					}
					, "작성일": "2012-03-03"
					, "작성시": "17:35"
					, "조회수": 256
					, "글권한": "Y"
					, "회원구분":"002"
					, "내용": "<img  src='../images/board/del.gif' alt=''/><br/><br/>코스 위에 정지하여 있는 흰 볼을 지팡이 모양의 클럽으로 잇달아 쳐서 정해진 홀(球孔)에 넣어 그때까지 소요된 타수(打數)의 많고 적음으로 우열을 겨루는 경기이다. 골프코스는 들판·구릉·산림 등 66만∼100만㎡의 넓은 지역을 이용하여 정형(整形)되어 있고, 해변에 만들어지는 시사이드 코스와 내륙에 만들어지는 인랜드 코스가 있다."
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			}, 500);
			
		}
		, getClubData: function(){
			return this.clubData;
		}
		, setClubRequest: function(key, val){
			this.clubRequest[key] = val;
		}
		, getClubRequest: function(key){
			return 	this.clubRequest[key];
		}
		, getClubMemberList: function(){
			var thisClass = this;
			var szClub_seq = this.getClubRequest("클럽키");
			var szUser = this.getClubMemberRequest("검색어");
			
			//ajax
			setTimeout(function(){
				thisClass.clubMemberPageData = {
					"전체수" : 11
				}
				
				thisClass.clubMemberList = [{
					"회원키": "6"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "100"
					,"댓글수" : "55"
					,"방문수" : "10"
					,"가입일" : "2012-03-08"
					,"최종방문일" : "2012-05-01"
				}
				,{
					"회원키": "5"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "101"
					,"댓글수" : "54"
					,"방문수" : "11"
					,"가입일" : "2012-03-07"
					,"최종방문일" : "2012-05-02"
				}
				,{
					"회원키": "4"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "102"
					,"댓글수" : "53"
					,"방문수" : "12"
					,"가입일" : "2012-03-06"
					,"최종방문일" : "2012-05-03"
				}
				,{
					"회원키": "3"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "103"
					,"댓글수" : "52"
					,"방문수" : "13"
					,"가입일" : "2012-03-05"
					,"최종방문일" : "2012-05-04"
				}
				,{
					"회원키": "2"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "104"
					,"댓글수" : "51"
					,"방문수" : "14"
					,"가입일" : "2012-03-04"
					,"최종방문일" : "2012-05-05"
				}
				,{
					"회원키": "1"
					,"회원정보" : {
						"이미지" : "http://192.168.0.82:8080/sky72web/images/board/default_50_50.gif"
						,"닉네임": "오스케 짱말짱"
					}
					,"회원구분":"일반회원"
					,"게시글수" : "150"
					,"댓글수" : "50"
					,"방문수" : "15"
					,"가입일" : "2012-03-03"
					,"최종방문일" : "2012-05-06"
				}
				]
				
				$(thisClass).trigger("evtFinishClubMemberList");
			}, 500);
		}
		, getClubMemberData: function(){
			return this.clubMemberList;	
		}
		, setClubMemberRequest: function(key, val){
			this.clubMemberRequest[key] = val;
		}
		, getClubMemberRequest: function(key){
			return 	this.clubMemberRequest[key];
		}
		, getClubMemberPageData: function(){
			return this.clubMemberPageData;
		}
		, getTransferSubmit: function(){
			var thisClass = this;
			var board_seq = this.getClubRequest("클럽키");
			var user_seq = this.getClubRequest("회원키");
			
			var ret = "";
			//ajax
			setTimeout(function(){
				ret = {
					"응답코드":"0000"
					, "메세지":"성공"
				}
				
				$(thisClass).trigger("evtFinishClubTransfer", [ret]);
			}, 500);
		}
		, getClubUserOut: function(){
			var thisClass = this;
			var board_seq = this.getClubRequest("클럽키");
			var user_seq = this.getClubRequest("회원키");
			
			var ret = "";
			//ajax
			setTimeout(function(){
				ret = {
					"응답코드":"0000"
					, "메세지":"성공"
				}
				
				$(thisClass).trigger("evtFinishClubUserOut", [ret]);
			}, 500);
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objClubTitle = null;
		this.objTopMenuInfo = null;
		this.objClubMember = null;
		this.objSearchType = null;
		this.objSearchText = null;
		this.objClubMemberPage= null;
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			
			this.objTopMenuInfo = new clsTopMenuInfo();
			this.objClubTitle = new clsClubTitle();
			
			this.objClubMember = new clsClubMember();
			this.objSearchType = new clsSearchType();
			this.objSearchText = new clsSearchText();
			this.objClubMemberPage = new document.objGlobal.clsPaging($("#idClubMemberPage"), 10, 10);
			
			//동호회정보/회원관리/탈퇴회원/가입승인/정기라운딩 탭설정
			this.objClubTitle.setIdx((document.objUtil.getParam("q_tab")?document.objUtil.getParam("q_tab"):0));
			
			this.objComm.setClubRequest("클럽키", document.objUtil.getParam("club_seq"));			
			
			this._bind();
			
			this.objComm.getClubInfo();
			this.objClubMember.spinStart();
			
		}
		, _bind: function(){
			var thisClass = this;

			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?club_seq=" +(thisClass.objComm.getClubRequest("클럽키"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));				
			});
			//navi 페이지 설정 시작 -->
			
			//동호회정보/회원관리/탈퇴회원/가입승인/정기라운딩 탭 이벤트설정
			this.objClubTitle.$getsBtnTab().click(function(event){
				event.preventDefault();
				var idx = thisClass.objClubTitle.$getsBtnTab().index($(this));
				switch(idx){
					case 0 : document.location.href = "club_admin_info.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 1 : document.location.href = "club_admin_mem.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 2 : document.location.href = "club_admin_out.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 3 : document.location.href = "club_admin_join.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
					case 4 : document.location.href = "club_admin_rd.html?club_seq=" + thisClass.objComm.getClubRequest("클럽키") + "&q_tab=" + idx;	break;
				}
				
			});
			//동호회정보 완료이벤트
			$(this.objComm).bind("evtFinishClubInfo", function(){
				thisClass.objComm.setClubMemberRequest("페이지", 1);
				
				thisClass.objClubMember.spinStart();
				thisClass.objClubMemberPage.setPage(1);
				thisClass.objClubMember.spinStart();
				thisClass.objComm.getClubMemberList();								
			});
			//동호회회원목록조회 완료이벤트
			$(this.objComm).bind("evtFinishClubMemberList", function(){
				var listData = thisClass.objComm.getClubMemberData();
				var pageData = thisClass.objComm.getClubMemberPageData();
				
				thisClass.objClubMember.setData(listData);
				thisClass.objClubMember.spinEnd();
				
				// 페이지 부에 전체 수설정 
				thisClass.objClubMemberPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubMemberPage.display();
			});
			//동호회양도 완료이벤트
			$(this.objComm).bind("evtFinishClubTransfer", function(event, ret){
				alert(ret["메세지"]);
				
				thisClass.objComm.getClubInfo();
				thisClass.objClubMember.spinStart();
			});
			//강제탈퇴 완료이벤트
			$(this.objComm).bind("evtFinishClubUserOut", function(event, ret){
				alert(ret["메세지"]);
				
				thisClass.objComm.getClubInfo();
				thisClass.objClubMember.spinStart();
			});
			
			//검색 버튼 이벤트
			$("article.golf_board_search :image").click(function(event){
				event.preventDefault();
				
				/*
				if(!thisClass.objSearchText.isValid() & thisClass.objSearchType.getValue() != 0){
					alert(thisClass.objSearchText.getError());
					thisClass.objSearchText.focus();
					return;
				}
				*/
				
			   thisClass.objComm.setClubMemberRequest("검색종류", thisClass.objSearchType.getValue());
				thisClass.objComm.setClubMemberRequest("검색어", thisClass.objSearchText.getValue());
				thisClass.objComm.setClubMemberRequest("페이지", 1);
				
				thisClass.objClubMemberPage.setPage(1);
				thisClass.objClubMember.spinStart();
				thisClass.objComm.getClubMemberList();
			});
			
			// 페이지이동 이벤트
			$(this.objClubMemberPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubMemberRequest("페이지", thisPage);
				thisClass.objClubMember.spinStart();
				thisClass.objComm.getClubMemberList();
			});
			
			//등급변경 버튼 이벤트
			$("div#container section#main_content div.golf_board article.btn_area a:eq(0)").click(function(event){
				event.preventDefault();
				alert("등급변경 팝업!!");
			});
			//동호회양도 버튼 이벤트
			$("div#container section#main_content div.golf_board article.btn_area a:eq(1)").click(function(event){
				event.preventDefault();
				
				//선택된 키값
				var key = thisClass.objClubMember.getCheckBoxKey();
								
				//vaildate
				if(thisClass.objComm.clubData["회원구분"] != "001") {
					alert("개설자외에는 양도기능을 사용할 수 없습니다.");
					return;
				}
				if(!key["회원키"]) {
					alert("선택된 회원이 없습니다." + key["회원키"]);
					return;
				}
				if(key["회원키"].split(",").length>1){
					alert("동호회양도는 다중선택이 불가능합니다.개별회원선택후 진행해주세요.");
					return;
				}
				if(!confirm("[" + key["닉네임"] + "] 회원에게 동호회를 양도하시겠습니까?")) {
					return;
				}				
				
				thisClass.objComm.setClubRequest("회원키",key["회원키"]);
				thisClass.objComm.getTransferSubmit();
				
			});
			//강제탈퇴버튼 이벤트
			$("div#container section#main_content div.golf_board article.btn_area a:eq(2)").click(function(event){
				event.preventDefault();
				//선택된 키값
				var key = thisClass.objClubMember.getCheckBoxKey();
								
				//vaildate
				if(thisClass.objComm.clubData["회원구분"] != "001" && thisClass.objComm.clubData["회원구분"] != "002") {
					alert("개설자/운영진 외에는 양도기능을 사용할 수 없습니다.");
					return;
				}
				if(!key["회원키"]) {
					alert("선택된 회원이 없습니다." + key["회원키"]);
					return;
				}
								
				thisClass.objComm.setClubRequest("회원키",key["회원키"]);
				thisClass.objComm.getClubUserOut();
			});
			
			//전체선택
			$("div#container section#main_content div.golf_board article.board_list table.clublist_w  thead tr th input").click(function(event){
				//event.preventDefault();
				//제목줄 체크상태
				var key = ($(this).attr("checked")?true:false);
				$(this).prop("checked", key);				
				thisClass.objClubMember.setChecked(key);
			});
		}
	}
	
	document.objControl = new clsControl();
});